package cn.jji8.createX.function.wand.canPlaceBlock;

import cn.jji8.createX.build.Build;
import cn.jji8.createX.function.fill.canPlaceBlock.FillCanPlaceBlock;
import cn.jji8.createX.build.LoadRes;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class WandFunctionBook {
    //每次最大替换数量
    public static int maximum = 100;
    public static Boolean airReplace = true;

    Block block;
    WandFunctionState wandFunctionState;
    public WandFunctionBook(Block block, WandFunctionState wandFunctionState) {
        this.block = block;
        this.wandFunctionState = wandFunctionState;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof WandFunctionBook){
            return ((WandFunctionBook) obj).block.equals(block);
        }
        return false;
    }

    public List<WandFunctionBook> dig(){
        //如果方块不是想替换的方块
        if(block.getType()!=wandFunctionState.bookMaterial){
            return null;
        }
        //领地权限
        if(!LoadRes.isCanBuild(wandFunctionState.player,block.getLocation())){
            return null;
        }
        List<WandFunctionBook> list = new ArrayList<>();
        //最大数量
        if(wandFunctionState.count>maximum){
            wandFunctionState.setCancel(true);
            return list;
        }
        //扣材料选项
        if(FillCanPlaceBlock.MaterialsAreNeeded){
            //扣材料
            if(!wandFunctionState.wand.buckle(wandFunctionState.replaceMaterial)){
                //取消掉
                wandFunctionState.setCancel(true);
                wandFunctionState.wandFunction.sendMessage = "§d魔棒工具正在工作,已为你替换§e"+wandFunctionState.count+"§d个方块。§l§c材料不足！ 请补充材料。";
                return list;
            }
        }
        //判断是否还有挖掘工具
        ItemStack itemStack = wandFunctionState.wand.iMiningTools;
        //挖掘选项
        if(FillCanPlaceBlock.UsingMiningTools){
            if(Material.AIR.equals(itemStack.getType())){
                //取消掉
                wandFunctionState.setCancel(true);
                wandFunctionState.wandFunction.sendMessage = "§d魔棒工具正在工作,已为你替换§e"+wandFunctionState.count+"§d个方块。§l§c没有挖掘工具！ 请补充挖掘工具。";
                return list;
            }
        }

        //判断是否可以挖掘
        if (!Build.isPlayerCanBreak(block,wandFunctionState.player)) {
            return list;
        }
        //显示破坏效果
        wandFunctionState.playEffectSTEP_SOUND(block);
        //挖掉之前的方块
        if(FillCanPlaceBlock.TheExcavatedBlockFalls){
            block.breakNaturally(wandFunctionState.wand.iMiningTools);
        }
        //减少工具耐久
        ItemMeta itemMeta = wandFunctionState.wand.iMiningTools.getItemMeta();
        if(itemMeta instanceof Damageable){
            Damageable damageable = ((Damageable)itemMeta);
            damageable.setDamage(damageable.getDamage()+1);
            //如果坏掉了，就设置成空气
            if(damageable.getDamage()>wandFunctionState.wand.iMiningTools.getType().getMaxDurability()){
                wandFunctionState.wand.iMiningTools = new ItemStack(Material.AIR);
            }
        }
        wandFunctionState.wand.iMiningTools.setItemMeta(itemMeta);
        //刷新显示的工具
        wandFunctionState.wand.MiningToolsButton.Refresh();

        //判断是否可以放置
        if (!Build.isPlayerCanPlace(block,block,new ItemStack(wandFunctionState.replaceMaterial),wandFunctionState.player)) {
            return list;
        }
        //填充方块
        block.setType(wandFunctionState.replaceMaterial);

        //查找周围的方块
        { Block block = this.block.getRelative(BlockFace.DOWN);
            if(block.getType().equals(wandFunctionState.bookMaterial)){
                list.add(new WandFunctionBook(block,wandFunctionState));
            } }
        { Block block = this.block.getRelative(BlockFace.EAST);
            if(block.getType().equals(wandFunctionState.bookMaterial)){
                list.add(new WandFunctionBook(block,wandFunctionState));
            } }
        { Block block = this.block.getRelative(BlockFace.NORTH);
            if(block.getType().equals(wandFunctionState.bookMaterial)){
                list.add(new WandFunctionBook(block,wandFunctionState));
            } }
        { Block block = this.block.getRelative(BlockFace.SOUTH);
            if(block.getType().equals(wandFunctionState.bookMaterial)){
                list.add(new WandFunctionBook(block,wandFunctionState));
            } }
        { Block block = this.block.getRelative(BlockFace.UP);
            if(block.getType().equals(wandFunctionState.bookMaterial)){
                list.add(new WandFunctionBook(block,wandFunctionState));
            } }
        { Block block = this.block.getRelative(BlockFace.WEST);
            if(block.getType().equals(wandFunctionState.bookMaterial)){
                list.add(new WandFunctionBook(block,wandFunctionState));
            } }
        wandFunctionState.count++;
        return list;
    }
}
